/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.smart.hawk.model.caller;

<<<<<<< HEAD
=======
import co.com.smart.connection.model.manager.JDBCResourceManager;
import co.com.smart.hawk.dto.DatosMaestrosDTO;
import co.com.smart.hawk.dto.MaestrosDTO;
>>>>>>> hawk_menu
import co.com.smart.hawk.dto.SitiosDTO;
import co.com.smart.hawk.dto.UsuarioDTO;
import co.com.smart.hawk.utilities.HawkSessionMgr;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Cesar Beltran
 */
public class SitiosCaller extends JDBCResourceManager{

    public SitiosCaller() {
        super();
    }
    
    /**
     * Almacena los sitios
     * @param sitios
     * @return
     * @throws Throwable 
     */
    public SitiosDTO grabarSitios(SitiosDTO sitios)throws Throwable{
        SitiosDTO returnObject = new SitiosDTO();
        
        Connection conn = null;
        CallableStatement call = null;
        UsuarioDTO user = (UsuarioDTO)HawkSessionMgr.getInstance().getSession("USER_SESSION");
        
        try {
            
            conn = getConnection();
            call = conn.prepareCall("{call GrabarSitios(?,?,?,?,?,?)}");
            
            if(sitios.getScsitios() != null){
                call.setBigDecimal(1, sitios.getScsitios());
            }else{
                call.setInt(1, java.sql.Types.NULL);
            }
            call.setString(2, sitios.getCositios());
            call.setString(3, sitios.getDssitios());
            call.setString(4, user.getCocodigo());
            call.registerOutParameter(5, java.sql.Types.VARCHAR);
            call.registerOutParameter(6, java.sql.Types.VARCHAR);
            call.executeUpdate();
            returnObject.setDsmensaje(call.getString(5));
            returnObject.setCodigo(call.getString(6));
            
        } catch (SQLException e) {
            throw  e;
        }finally{
            closeResources(conn, call);
        }
        
        
        return returnObject;
    }
      
       /**
     * Consulta los datos maestros apartir del maerstro filtrado
     * @param filtros
     * @return 
     */
    public List<SitiosDTO> consultarSitios(SitiosDTO filtros) throws Throwable {

        ArrayList<SitiosDTO> listsitios = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String sql = " select sm_scsitios,sm_cositios,sm_dssitios "
                    +" from Smart_sitios a ";
        try {
            conn = getConnection();
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            
            if (rs!=null){
                while(rs.next()){
                    SitiosDTO sitios = new SitiosDTO();
                    sitios.setScsitios(rs.getBigDecimal("sm_scsitios"));
                    sitios.setCositios(rs.getString("sm_cositios"));
                    sitios.setDssitios(rs.getString("sm_dssitios"));
                   
                    listsitios.add(sitios);
                }
            }

        } catch (SQLException sqle) {
            throw sqle;
        } finally {
            closeResources(conn, pst, rs);
        }

        return listsitios;

    }
}
